package com.clear.honghefinance.biz.modules.assist_calculate_summary.handler;

import com.clear.honghefinance.biz.modules.assist_calculate.enums.AssistCalculateCategoryCodeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 辅助核算处理工厂
 */
@Component
public class AssistCalculateHandlerFactory implements InitializingBean {

    @Resource
    private List<AssistCalculateHandler> assistCalculateHandlerList;

    // 定义一个map，缓存所有的 策略
    private final Map<AssistCalculateCategoryCodeEnum, AssistCalculateHandler> handlerMap = new HashMap<>();

    public AssistCalculateHandler getHandler(String cateCode) {
        AssistCalculateCategoryCodeEnum categoryCodeEnum = AssistCalculateCategoryCodeEnum.getByCode(cateCode);
        return handlerMap.get(categoryCodeEnum);
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        for (AssistCalculateHandler assistCalculateHandler : assistCalculateHandlerList) {
            handlerMap.put(assistCalculateHandler.getHandlerType(), assistCalculateHandler);
        }
    }
}
